﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace zUpLoad.Common
{
    public class cDll1
    {
        public static Object Main00(string lpFileName, string Namespace, string ClassName,
    string lpprocName, object[] ObjArray_Parmeter) 
        { 
            string callingDomainName = AppDomain.CurrentDomain.FriendlyName;//Thread.GetDomain().FriendlyName; 
            //Console.WriteLine(callingDomainName); 
            AppDomain ad = AppDomain.CreateDomain("DLL Unload test");
            ProxyObject obj = (ProxyObject)ad.CreateInstanceFromAndUnwrap("zUpLoad.exe", "zUpLoad.Common.cDll1.ProxyObject");            
            obj.LoadAssembly(lpFileName);
            Object rVale;
            rVale=obj.Invoke(ClassName, lpprocName, ObjArray_Parmeter); 
            AppDomain.Unload(ad); 
            obj = null;
            return rVale;
            //Console.ReadLine(); 
        } 
    }
    public class ProxyObject : MarshalByRefObject
    {
        Assembly assembly = null;
        public void LoadAssembly(string lpFileName)
        {
            assembly = Assembly.LoadFile(lpFileName);
        }
        public object Invoke(string fullClassName, string methodName, params Object[] args)
        {
            if (assembly == null)
            { return null; }
            Type tp = assembly.GetType(fullClassName);
            if (tp == null)
            { return null; }
            MethodInfo method = tp.GetMethod(methodName);
            if (method == null)
            { return null; }
            Object obj = Activator.CreateInstance(tp);
            return method.Invoke(obj, args);
        }
    }
} 